import {
  checkAuthStatus,
  clearAuthData,
  getLocalAuthData,
  getUserRole
} from "@/auth";
import { createRouter, createWebHistory } from "vue-router";
import { UserRoleEnum } from "@/types/global-types.d";
import Course from "../components/Course.vue";
import Department from "../components/Department.vue";
import Enrollment from "../components/Enrollment.vue";
import Forbidden from "@/views/Forbidden.vue";
import Home from "../components/Home.vue";
import Login from "@/views/auth/Login.vue";
import Student from "../components/Student.vue";
import Teacher from "../components/Teacher.vue";
import CourseSelection from "@/components/CourseSelection.vue";
import CourseSelectionResults from "@/components/CourseSelectionResults.vue";

const routes = [
  {
    path: "/",
    name: "main",
    component: Home,
    meta: {
      requireAuth: true,
      allowedRoles: [
        UserRoleEnum.ADMIN,
        UserRoleEnum.TEACHER,
        UserRoleEnum.STUDENT
      ],
      layout: "main"
    }
  },
  {
    path: "/course-selection",
    name: "Course-selection",
    component: CourseSelection,
    meta: {
      requireAuth: true,
      allowedRoles: [UserRoleEnum.ADMIN, UserRoleEnum.STUDENT],
      layout: "main"
    }
  },
  {
    path: "/course-selection-results",
    name: "Course-selection-results",
    component: CourseSelectionResults,
    meta: {
      requireAuth: true,
      allowedRoles: [UserRoleEnum.ADMIN, UserRoleEnum.STUDENT],
      layout: "main"
    }
  },
  {
    path: "/login",
    name: "Login",
    component: Login,
    meta: {
      requireAuth: false,
      layout: "empty"
    }
  },
  {
    path: "/home",
    name: "Home",
    component: Home,
    meta: {
      requireAuth: true,
      allowedRoles: [
        UserRoleEnum.ADMIN,
        UserRoleEnum.TEACHER,
        UserRoleEnum.STUDENT
      ],
      layout: "main"
    }
  },
  {
    path: "/departments",
    name: "Department",
    component: Department,
    meta: {
      requireAuth: true,
      allowedRoles: [UserRoleEnum.ADMIN, UserRoleEnum.TEACHER], // 添加角色权限
      layout: "main"
    }
  },
  {
    path: "/students",
    name: "Student",
    component: Student,
    meta: {
      requireAuth: true,
      allowedRoles: [UserRoleEnum.ADMIN, UserRoleEnum.TEACHER], // 添加角色权限
      layout: "main"
    }
  },
  {
    path: "/teachers",
    name: "Teacher",
    component: Teacher,
    meta: {
      requireAuth: true,
      allowedRoles: [UserRoleEnum.ADMIN], // 添加角色权限
      layout: "main"
    }
  },
  {
    path: "/courses",
    name: "Course",
    component: Course,
    meta: {
      requireAuth: true,
      allowedRoles: [UserRoleEnum.ADMIN, UserRoleEnum.TEACHER], // 添加角色权限
      layout: "main"
    }
  },
  {
    path: "/enrollments",
    name: "Enrollment",
    component: Enrollment,
    meta: {
      requireAuth: true,
      allowedRoles: [
        UserRoleEnum.ADMIN,
        // UserRoleEnum.TEACHER,
        UserRoleEnum.STUDENT // 选课是仅给学生的功能
      ], // 添加角色权限
      layout: "main"
    }
  },
  {
    path: "/forbidden",
    name: "Forbidden",
    component: Forbidden,
    meta: {
      requireAuth: false,
      layout: "empty"
    }
  }
];

const router = createRouter({
  history: createWebHistory(),
  routes
});

// 添加路由守卫
router.beforeEach((to, from, next) => {
  // 检查目标路由是否需要认证
  if (to.meta.requireAuth) {
    // 检查用户是否已登录
    const isAuthenticated = checkAuthStatus();

    if (isAuthenticated) {
      // 获取用户角色
      const userRole = getUserRole();

      // 检查用户是否有权限访问该路由
      const allowedRoles = to.meta.allowedRoles as UserRoleEnum[];

      if (allowedRoles && allowedRoles.includes(userRole as UserRoleEnum)) {
        next(); // 有权限则继续
      } else {
        // 无权限则重定向到无权限页面
        next("/forbidden");
      }
    } else {
      // 未登录则重定向到登录页
      next({
        path: "/login",
        query: { redirect: to.fullPath } // 保存目标路径用于登录后重定向
      });
    }
  } else {
    // 不需要认证的路由直接放行
    next();
  }
});

export default router;
